<?php

namespace app\common\model;

use app\common\model\BaseModel;


class VipOrder extends BaseModel
{
    // 表名
    protected $name = 'vip_order';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        //'status_text',
        'create_time_text'
    ];
    

    
    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1'), '10' => __('Status 10')];
    }


    /*public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }*/


    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id')->joinType('LEFT');
    }
    public function serviceBasic()
    {
        return $this->belongsTo('ServiceBasic', 'code', 'code')->joinType('LEFT');
    }

    public static function buildAccess($vip_info){
        $code=[
            'ward',
            'surgery_ward',
            'special_ward',
            'escort_vip',
            'escort',
            'escort_halfday',
            'register',
            'inquiry',
            'document',
            'medicine',
            'records',
            'escort_overtime',
            'severe_illness',
            'oversea',
            'private_doctor',
            'famous_doctor'
        ];

        foreach ($code as $c){
            $datas=[];
            if($vip_info[$c.'_last']!=0){
                for($counter=1;$counter<=$vip_info[$c.'_last'];$counter++){
                    $datas[]=[
                        'vip_user_id'=>$vip_info['id'],
                        'user_id'=>$vip_info['user_id'],
                        'order_id'=>'',
                        'code'=>$c,
                        'status'=>0,
                        'free_sn'=>strlen($counter)>=4?:str_pad($counter,4,'0',STR_PAD_LEFT),
                        'create_time'=>time()
                    ];
                }
                self::insertAll($datas);
            }
        }
    }
}
